//获取应用实例
let app = getApp();
let httptool=require("../../utils/zry.httptool.js");
Page({
  data:{
    createState:'立即创建',
    cirdleId:'',
    CircleName:'',
    Description:'',
    circle: { CircleName: '', Description:''},
    LogoUrl:'../../images/deflogo.png',
    editmodel:0,
    msgprop:''//消息提示
  },
  onLoad: function (options) {
    let that = this;
    let editmodel=options.editmodel;
    let cirdleId=options.ID;
    that.setData({editmodel:editmodel});
    if(editmodel==1){
      that.setData({ createState: '确定修改', cirdleId: cirdleId});
    }
    that.getCircleDetail();
    httptool.login(function(){
      // let headImg = app.getStorage('headImg');
      // that.setData({LogoUrl:headImg });
    });   
  },
  nameTextarea:function(e){
    this.data.circle.CircleName = e.detail.value;
    this.setData({ circle: this.data.circle});
  },
  binddescArea:function(e){
    this.data.circle.Description = e.detail.value;
    this.setData({ circle: this.data.circle});
  },
  changeLogo:function(){
    let self=this;
    let rootUrl = httptool.getRootUrl();
    wx.chooseImage({
  　　count: 1, //最多可以选择的图片数，默认为9
  　　sizeType: ['orignal', 'compressed'], //original 原图，compressed 压缩图，默认二者都有
  　　sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有
  　　success: function (res) { 
      self.setData({ LogoUrl: res.tempFilePaths[0]});      
      } //成功则返回图片的本地文件路径列表 tempFilePaths
    })
  },
  submitMeba:function(e){
    let self = this;
    let circle = self.data.circle;
    if (circle.CircleName == '') {
      app.showNotify(self, "擂盟名称不能为空！");
      return false;
    }else if (!app.validate(circle.CircleName, 'leim')) {
      app.showNotify(self, "擂盟名称不能包含特殊字符和空格！" );
      return false;
    } else if (circle.Description == '') {
      app.showNotify(self, "擂盟描述不能为空！");
      return false;
    }
    if (self.data.LogoUrl =="../../images/deflogo.png"){
      app.showNotify(self, "请先上传擂Logo");
      return false;
    }
    let rootUrl = httptool.getRootUrl();
    if (self.data.LogoUrl.indexOf(rootUrl)==0){
      self.addCircle("");
    }else{
      self.upload();
    }
    
  },
  upload: function () {
    let self = this;
    let rootUrl = httptool.getRootUrl();
    wx.uploadFile({
      url: rootUrl + 'api/ImgUpload/ImgUpload', //开发者服务器 url
      filePath: self.data.LogoUrl,//要上传文件资源的路径
      name: 'file', //文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
      formData: { //HTTP 请求中其他额外的 form data
        savepath: "File\\Circle\\",
        token: "62ccad6d-9611-41c5-96f6-08ec0ada0f68"
      },
      success: function (res) {
        var data = JSON.parse(res.data);
        if (data.suc) {
          self.addCircle(rootUrl + data.data);
        }
      }
    })
  },
  addCircle: function (logoUrl){
    let self=this;
    let circle = self.data.circle;
    if (logoUrl)
      circle.LogoUrl = logoUrl;
    else
      circle.LogoUrl = self.data.LogoUrl;
    circle.CircleName = circle.CircleName +'擂盟';
    circle.OnwerId = app.getStorage("customerId");
    if (self.data.editmodel==1){
      httptool.ajaxPost("api/Circle/UpdateCircle", circle, function (res) {
        if (res.suc) {
          app.showNotify(self, '修改成功',function(){
            wx.navigateBack(); 
          });            
        } else {
          app.showNotify(self, res.msg);
        }
      });
    }else{
      httptool.ajaxPost("api/Circle/AddCircle", circle, function (res) {
        if (res.suc) {
          wx.redirectTo({
            url: '/pages/feedresult/feedresult?type=' + 1,
          });
        } else {
          app.showNotify(self, res.msg);
        }
      });
    }

  },
  getCircleDetail:function(e){
    let that=this;
     httptool.ajaxPost("api/Circle/GetCircleDetail", {ID:that.data.cirdleId}, function (res) {
      if (res.suc) {
          let datas=res.data;
          datas.CircleName = datas.CircleName.substring(0, datas.CircleName.length-2);
          that.setData({
            CircleName: datas.CircleName, circle: datas,
          Description:datas.Description,LogoUrl:datas.LogoUrl});
      } else {
        app.showNotify(self, res.msg);
      }
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})